const express = require('express')
const router = express.Router()
const Resume = require('../db/model/ResumeModel')
const { mySend, Vtoken, checkParams } = require('../utils/index')
const { to } = require('await-to-js')

router.post('/add', async (req, res) => {
    const { name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, wordUrl,  pdfUrl, status, token } = req.body
    let checked = checkParams({ name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, status, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    let data = {name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, wordUrl,  pdfUrl, status}
    data.userId = user._id
    let [err, result] = await to(Resume.insertMany(data))
    if(err) return mySend(res, 1, '添加失败', err)
    return mySend(res, 0, '添加成功', result)
})

router.post('/update', async (req, res) => {
    const { _id, name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, wordUrl,  pdfUrl, status, token } = req.body
    let checked = checkParams({ _id, name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, status, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    let data = {name, username, userImg, basicInfo, selfAssessment, abilities, experiences, projects, demos, edus, certificates, wordUrl,  pdfUrl, status}
    let [err, result] = await to(Resume.updateOne({_id}, {...data}))
    if(err) return mySend(res, 1, '修改失败', err)
    return mySend(res, 0, '修改成功', result)
})

router.post('/delete', async (req, res) => {
    const { _id, token } = req.body
    let checked = checkParams({ _id, token })
    if (checked) return mySend(res, 1, checked)
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')
    let [err, result] = await to(Resume.deleteOne({_id}))
    if(err) return mySend(res, 1, '删除失败', err)
    if(result.deletedCount === 0) return mySend(res, 1, '删除失败', result)
    return mySend(res, 0, '删除成功', result)
})

router.post('/list', async (req, res) => {
    const { token, current = 1, size = 10 } = req.body
    let user = await Vtoken(token)
    if (!user) return mySend(res, 314, 'token不存在或已过期')

    let query = {userId: user._id}
    let [err, total] = await to(Resume.countDocuments(query))
    let [err1, result] = await to(Resume.find(query).sort({ctime: -1}).skip((current - 1) * size).limit(size))
    if(err1) return mySend(res, 1, '查询失败', err1)
    let body = {
        total,
        current, 
        size,
        list: result
    }
    return mySend(res, 0, 'success', body)
})

router.post('/listOne', async (req, res) => {
    const { _id } = req.body
    let checked = checkParams({ _id})
    if (checked) return mySend(res, 1, checked)
    // let user = await Vtoken(token)
    // if (!user) return mySend(res, 314, 'token不存在或已过期')

    let [err, result] = await to(Resume.find({_id}))
    if(err) return mySend(res, 1, '查询失败', err)
    return mySend(res, 0, 'success', result[0])
})

module.exports = router

